using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DABL;

public partial class Admin_ManageUsers : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            gViewUsers.DataSource = Membership.GetAllUsers();
            gViewUsers.DataBind();
        }
    }

    public void chkActive_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox checkBox = (CheckBox)sender;
        GridViewRow item = (GridViewRow)checkBox.Parent.Parent;
        Label lblUser = (Label)item.FindControl("UserNameLink");

        MembershipUser user = Membership.GetUser(lblUser.Text);
        user.IsApproved = checkBox.Checked;
        Membership.UpdateUser(user);
    }

    private void bindrptRoles(string userName)
    {
        rptAccount.DataSource = QuestionnaireController.AccountsGet();
        rptAccount.DataBind();
        IDataReader  dr = QuestionnaireController.UserAccountsGet(Membership.GetUser(userName).ProviderUserKey.ToString());
        CheckBox cblRoles = null;
        while(dr.Read())
        {
            for(int i = 0; i < rptAccount.Items.Count; i++)
            {
                cblRoles = new CheckBox();
                cblRoles = (CheckBox)rptAccount.Items[i].FindControl("cblRoles");
                if(dr.GetString(1) == cblRoles.Text)
                {
                    cblRoles.Checked = true;
                }
            }
        }
        AddToRole.Text = userName;
    }

    protected void gViewUsers_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        switch(e.CommandName)
        {
            case "EditAccounts":
            {
                divRoles.Visible = true;
                bindrptRoles(e.CommandArgument.ToString());
            }
            break;
           
        }
    }

    protected void cblRoles_CheckedChanged(object sender, EventArgs e)
    {
        try
        {
            CheckBox checkBox = (CheckBox)sender;
            RepeaterItem rptItem = (RepeaterItem)checkBox.Parent;
            string user = AddToRole.Text;
            Int32 AccountID = Convert.ToInt32(((HiddenField)rptItem.FindControl("hfAccountID")).Value);
            if(checkBox.Checked)
            {

                QuestionnaireController.UserAccountsAdd(Membership.GetUser(user).ProviderUserKey.ToString(), AccountID);
                bindrptRoles(user);
            }
            else
            {
                QuestionnaireController.UserAccountsRemove(Membership.GetUser(user).ProviderUserKey.ToString(), AccountID);
                bindrptRoles(user);
            }
        }
        catch(Exception ex)
        {
            AddToRole.Text = ex.Message;
        }
    }


    protected void btnAccount_Click(object sender, EventArgs e)
    {
        if(txtAccount.Text != "")
        {
                QuestionnaireController.AccountsAdd(txtAccount.Text);
                txtAccount.Text = "";
        }

        bindrptRoles(AddToRole.Text);
    }
}
